﻿using System;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Content.PM;
using Capstone.Models;

namespace Capstone.Droid
{
    [Activity(Label = "ActivityCustomUrlSchemeInterceptor", Exported = true, NoHistory = true, LaunchMode = LaunchMode.SingleTop)]
    [ 
        IntentFilter
        (
            actions: new[] { Intent.ActionView },
            Categories = new[]
                    {
                        Intent.CategoryDefault,
                        Intent.CategoryBrowsable
                    },
            DataSchemes = new[]
                    {
                        "com.companyname.capstone"
                    },
            DataPaths = new[]
                    {
						"/oauth2redirect",          // Google
					},
            AutoVerify = true
        )
    ]
    public class ActivityCustomUrlSchemeInterceptor : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            global::Android.Net.Uri uri_android = Intent.Data;

            // Convert iOS NSUrl to C#/netxf/BCL System.Uri - common API
            Uri uri_netfx = new Uri(uri_android.ToString());

            // Load redirect_url Page
            AuthenticationState.Authenticator.OnPageLoading(uri_netfx);

            var intent = new Intent(this, typeof(MainActivity));
            intent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
            StartActivity(intent);

            this.Finish();

            return;
        }
    }
}